<template>
  <div id="app">
    <div class="head" v-if="!this.isShowTabbar">
      <van-nav-bar
        :title="title"
        left-text="返回"
        left-arrow
        @click-left="$router.go(-1)"
      ></van-nav-bar>
    </div>
    <keep-alive include="Category">
      <router-view :key="$route.query.id" />
    </keep-alive>
    <van-tabbar route active-color="#C75559" v-if="isShowTabbar">
      <van-tabbar-item replace to="/home" icon="wap-home-o"
        >首页</van-tabbar-item
      >
      <van-tabbar-item replace to="/topic" icon="description"
        >专题</van-tabbar-item
      >
      <van-tabbar-item replace to="/category" icon="apps-o"
        >分类</van-tabbar-item
      >
      <van-tabbar-item replace to="/cart" icon="shopping-cart-o"
        >购物车</van-tabbar-item
      >
      <van-tabbar-item replace to="/mine" icon="friends-o"
        >我的</van-tabbar-item
      >
    </van-tabbar>
  </div>
</template>
<script>
export default {
  data() {
    return {
      isShowTabbar: true, // 是否显示底部tabbar
      title: "", // 顶部title,根据meta动态更新
    };
  },

  watch: {
    $route(to) {
      const paths = ["/home", "/topic", "/category", "/cart", "/mine"];
      let path = paths.find((item) => item === to.path);
      if (path) {
        this.isShowTabbar = true;
      } else {
        this.isShowTabbar = false;
        this.title = to.meta.title;
      }
    },
  },

  created() {
    // 模拟用户登录
    var userInfo = {
      openId: "66",

      nickName: "赵六",

      avatarUrl:
        "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIbWFEIJj8IpGeHM7dGic1aTFZALjWcMm9ltWfFiaQfVRYticWBfgGfzXWMt2EkJWiaicPtftHAlWxUibxQ/132",
    };

    var openId = userInfo.openId;

    localStorage.setItem("userInfo", JSON.stringify(userInfo));

    localStorage.setItem("openId", openId);
  },
};
</script>

<style>
html,
body {
  height: 100%;
}
ul,
li {
  list-style: none;
}

* {
  margin: 0;
  padding: 0;
}
#app {
  height: 100%;
  background-color: #f4f4f4;
}
</style>
